﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Summer.Model;
using Summer.Model.core;
using Summer.WebApi.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Summer.WebApi.Filter
{
    public class GlobalExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            Exception ex = context.Exception;
            string errMsg = "GlobalExceptionFilter-OnException：" + ex.Message;
            if (context.Exception.GetType() == typeof(ExecuteException))
            {
                //针对不同的自定义异常，做不同处理
                MsgModel<string> msgModel = new MsgModel<string>()
                {
                    Status = false,
                    Msg = errMsg,
                    Errcode = "AA001"
                };
                context.Result = new JsonResult(msgModel);
                context.ExceptionHandled = true;
            }
            else
            {
                context.Result = new JsonResult(errMsg);
                context.ExceptionHandled = true;
            }

            LogHelper.Error(errMsg);
        }
    }
}
